//
//  JYJVCOfSeach.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/8.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJVCOfSeach.h"
#import "JYJTAMacro.h"
#import "JYJVCOfSechContent.h"
#import "Masonry.h"
#import "BaseCollectionView.h"
#import "JYJHotDoorSeachCollectionViewCell.h"
#import "SelfSizeTool.h"
#import "JYJMusicMiniView.h"
@interface JYJVCOfSeach ()<UISearchBarDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UISearchBar *seachBar;
@property (nonatomic, copy) NSMutableString *content;
@property (nonatomic, strong) BaseCollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *titleMarr;
@end

@implementation JYJVCOfSeach

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    [self createSeachBar];
    [self createNavigationItem];
    [self createSubViews];
    self.titleMarr = [NSMutableArray arrayWithObjects:@"盗墓笔记",@"鬼吹灯",@"黑暗坡食人树",@"出牌",@"三体",@"哏小姐驾到",@"我了个槽",@"好妹妹坏坏说",@"闻所未闻",@"娱乐扒扒扒",@"拥你入眠",@"在异乡",@"音乐日记", nil];
    // Do any additional setup after loading the view.
}
- (void)createSeachBar {
    self.seachBar = [[UISearchBar alloc]initWithFrame:CGRectMake(90, 0, SCREEN_WIDTH - 180, 40)];
    self.navigationItem.titleView = self.seachBar;
    self.seachBar.barTintColor = [UIColor colorWithRed:0.31 green:0.34 blue:0.40 alpha:1.00];
    self.seachBar.delegate = self;
    self.seachBar.placeholder = @"搜索主播，栏目，节目";
    [self.seachBar setSearchBarStyle: UISearchBarStyleMinimal];
    for (UIView *subView in self.seachBar.subviews)
    {
        for (UIView *secondLevelSubview in subView.subviews){
            if ([secondLevelSubview isKindOfClass:[UITextField class]])
            {
                UITextField *searchBarTextField = (UITextField *)secondLevelSubview;
                //set font color here
                searchBarTextField.textColor = [UIColor colorWithRed:0.62 green:0.66 blue:0.73 alpha:1.00];
                break;
            }
        }
    }

}
// seachBar的协议方法
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    self.content = searchText.mutableCopy;
}
//点击键盘搜索时
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    JYJVCOfSechContent *seachContent = [[JYJVCOfSechContent alloc]init];
    [self.seachBar resignFirstResponder];
    seachContent.inPutWord = self.content;
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:seachContent animated:YES];
}
- (void)createNavigationItem {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(handleSeach)];
}
- (void)handleSeach {
    JYJVCOfSechContent *seachContent = [[JYJVCOfSechContent alloc]init];
    seachContent.inPutWord = self.content;
    [self.seachBar resignFirstResponder];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:seachContent animated:YES];

}
- (void)createSubViews{
    UIImageView *imageOfBack = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"search_bar"]];
    [self.view addSubview:imageOfBack];
    [imageOfBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(10);
        make.left.equalTo(self.view.mas_left).with.offset(20);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(SCREEN_HEIGHT / 2 + 100);
    }];
    UIImageView *imageOfBall = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"search_bar_circle"]];
    [self.view addSubview:imageOfBall];
    [imageOfBall mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).with.offset(120);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    UIView *colorView = [[UIView alloc]init];
    [self.view addSubview:colorView];
    colorView.backgroundColor =  [UIColor colorWithRed:0.34 green:0.34 blue:0.39 alpha:1.00];
    [colorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageOfBall.mas_right).with.offset(0);
        make.top.equalTo(self.view.mas_top).with.offset(130);
        make.right.equalTo(self.view.mas_right).with.offset(0);
        make.height.mas_equalTo(2);
    }];
    UILabel *redLabel = [[UILabel alloc]init];
    [self.view addSubview:redLabel];
    redLabel.text = @"热门搜索";
    redLabel.textColor = [UIColor colorWithRed:0.79 green:0.09 blue:0.15 alpha:1.00];
    [redLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(colorView.mas_top).with.offset(5);
        make.left.equalTo(imageOfBack.mas_right).with.offset(20);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(40);
    }];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageOfBack.mas_right).with.offset(0);
        make.top.equalTo(colorView.mas_bottom).with.offset(5);
        make.right.equalTo(self.view.mas_right).with.offset(-40);
        make.height.mas_equalTo(250);
    }];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[JYJHotDoorSeachCollectionViewCell class] forCellWithReuseIdentifier:@"Hot"];
    

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.titleMarr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJHotDoorSeachCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Hot" forIndexPath:indexPath];
    cell.titleLabel.text = self.titleMarr[indexPath.row];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.titleMarr[indexPath.row];
    CGFloat width = [SelfSizeTool selfSizeWidthString:title height:30 font:[UIFont systemFontOfSize:17]];
    return CGSizeMake(width + 5, 30);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.content = self.titleMarr[indexPath.row];
    self.seachBar.text = self.content;
    JYJVCOfSechContent *seachContent = [[JYJVCOfSechContent alloc]init];
    seachContent.inPutWord = self.content;
    [self.seachBar resignFirstResponder];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:seachContent animated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.hidesBottomBarWhenPushed = YES;
    [JYJMusicMiniView shareMiniView].hidden = YES;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
